<template>

	<view class="contain">
		<view class="content">
			<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="80">
				<!-- 代接送人 -->
				<u-form-item label="代接送人:" prop="userInfo.pickName" borderBottom ref="item1" required><u--input
						v-model="model1.userInfo.pickName" border="none"></u--input></u-form-item>
				<!-- 联系方式 -->
				<u-form-item label="联系方式:" prop="userInfo.pickPhone" borderBottom ref="item1" required><u--input
						v-model="model1.userInfo.pickPhone" border="none"></u--input></u-form-item>
				<!-- 身份证号 -->
				<u-form-item label="身份证号:" prop="userInfo.pickIdCard" borderBottom ref="item1" required><u--input
						v-model="model1.userInfo.pickIdCard " border="none"></u--input></u-form-item>
						<button @click="submit" class="btn">提交</button>
			</u--form>
		</view>
		<u-tabbar :value="tabbarIndex" @change="click" :fixed="true" :border="true" activeColor='#1cb8a7'>
			<u-tabbar-item text="申请" icon="edit-pen-fill" @click='clickTabbar'></u-tabbar-item>
			<u-tabbar-item text="列表" icon="file-text-fill" @click='clickTabbar'></u-tabbar-item>

		</u-tabbar>
	</view>
</template>

<script>
	var checkCode = function (val) {
	    var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	    var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
	    var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
	    var code = val.substring(17);
	    if(p.test(val)) {
	        var sum = 0;
	        for(var i=0;i<17;i++) {
	            sum += val[i]*factor[i];
	        }
	        if(parity[sum % 11] == code.toUpperCase()) {
	            return true;
	        }
	    }
	    return false;
	}
	var checkDate = function (val) {
	    var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
	    if(pattern.test(val)) {
	        var year = val.substring(0, 4);
	        var month = val.substring(4, 6);
	        var date = val.substring(6, 8);
	        var date2 = new Date(year+"-"+month+"-"+date);
	        if(date2 && date2.getMonth() == (parseInt(month) - 1)) {
	            return true;
	        }
	    }
	    return false;
	};
	var checkProv = function (val) {
	    var pattern = /^[1-9][0-9]/;
	    var provs = {11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门"};
	    if(pattern.test(val)) {
	        if(provs[val]) {
	            return true;
	        }
	    }
	    return false;
	};
	var checkID = function (val) {
	    if(checkCode(val)) {
	        var date = val.substring(6,14);
	        if(checkDate(date)) {
	            if(checkProv(val.substring(0,2))) {
	                return true;
	            }
	        }
	    }
	    return false;
	};
	
	
	import {
		saveOrUpdatePickChild
	} from '@/api/pickChild.js';
	import { util } from '@/utils/tools.js';
	import {
		save
	} from '@/api/FaultRepair.js';
	
	export default {
		options: { styleIsolation: 'shared' },//解决/deep/不生效**
		data() {
			return {
				model1: {
					userInfo: {
						pickName:'',
						pickPhone:'',
						pickIdCard:'',
					}
				},
				rules: {
					'userInfo.pickName': {
						type: 'string',
						required: true,
						message: '请填写代接人姓名',
					},
					'userInfo.pickPhone': [{
						required: true,
						message: '请输入手机号',
						trigger: ['change', 'blur'],
					},
					{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							return uni.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['change', 'blur'],
					}],
					
					'userInfo.pickIdCard': [{
						required: true,
						message: '请输入身份证号',
						trigger: ['change', 'blur'],
					},
					{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							console.log('返回校验',checkID(value))
							return checkID(value)
						},
						message: '身份证格式不正确',
						// 触发器可以同时用blur和change
						trigger: ['change', 'blur'],
					}]
					
				},
				tenantId:'',
				tabbarIndex: 0,
			};
		},
		onLoad() {
			this.tenantId= 'dddd';
		},
		onReady() {
			 this.$refs.uForm.setRules(this.rules);
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		},
		// 
		methods: {
			submit() {
				this.$refs.uForm.validate().then(async res => {
				this.model1.userInfo.studentId = this.$util.getChild().userId;
				this.model1.userInfo.studentName = this.$util.getChild().userName;
				this.model1.userInfo.classId= this.$util.getChild().classId;
				this.model1.userInfo.className = this.$util.getChild().className;
				uni.showLoading({
					title: '提交中...',
					mask: true
				})
				const {data}=await saveOrUpdatePickChild(this.model1.userInfo)
				if(data.code==0){
					uni.$u.toast('申请成功')
					uni.redirectTo({
						url:'list'
					})
					uni.hideLoading();
				}
				}).catch(errors => {
					uni.hideLoading();
				})
			},
			click() {
			
			},
			clickTabbar(e) {
				this.tabbarIndex = e
				console.log('eeeee', e)
				if (this.tabbarIndex == 0) {
					uni.redirectTo({
						url: '/pages/subPack/pickUp/add'
					})
				} else if (this.tabbarIndex == 1) {
					uni.redirectTo({
						url: '/pages/subPack/pickUp/list'
					})
				}
			}
		}
	};
</script>
<style lang="less" scoped>
	.contain {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}

	/deep/.u-icon__icon {
		font-size: 50rpx !important
	}
	/deep/.u-action-sheet {
		height: 200px;
		overflow: scroll;
	}
	.u-page__tag-item {
		display: inline-block;
		margin-right: 30rpx;
		margin-bottom: 10rpx;
		margin-top: 30rpx;
	}

	.content {
		padding: 30rpx 50rpx;
	}


	/deep/.uni-input-input {
		text-align: right;
	}

	/deep/.uni-input-placeholder {
		text-align: right;
	}

	/deep/.uni-textarea-placeholder {
		text-align: right;
	}

	/deep/.u-upload {
		align-items: end;
	}



	// 底部
	/deep/.u-tabbar--fixed {
		border-top: 1px solid #dadbde;
	}

	/deep/.u-icon__icon {
		// font-size: 48rpx!important
	}

	/deep/.u-tabbar-item__text {
		font-size: 26rpx
	}

	/deep/.u-form-item__body__right__content__slot {
		justify-content: end;
	}

	/deep/.u-textarea {
		text-align: end;
	}

	/deep/ .u-popup__content {
		padding-bottom: 20px !important;
	}
</style>